package com.uubx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uubx.exception.CustomerException;
import com.uubx.pojo.Policy;
import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.PolicyService;
import com.uubx.vo.PolicyListVo;
import com.uubx.vo.PolicyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author wyy
 * @Date 2020/12/28 9:18
 * @Version 1.0
 */
@Controller
@RequestMapping("/policy")
@ResponseBody
@Api(description = "保单表的相关接口")
public class PolicyController {

    @Autowired
    private PolicyService policyService;


    @RequestMapping(value = "/listWhere",method = RequestMethod.POST)
    @ApiOperation(value = "条件分页查询",notes = "条件分页查询",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result listWhere(@RequestBody PolicyVo policyVo) throws CustomerException {

       PageHelper.offsetPage(policyVo.getStartIndex(),policyVo.getPageSize());
        List<Map> list = policyService.findGoodsWhere(policyVo);

        PageInfo<Map> pageInfo = new PageInfo<>(list);

        if (pageInfo.getList().isEmpty()){
            throw new CustomerException(MessageCode.INSURANCE_EMPTY);
        }


         return ResultUtils.buildSuccess(pageInfo);
    }

    @RequestMapping(value = "/findId",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询所有数据",notes = "根据id查询所有数据",httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result findById(Long id) throws CustomerException {

        if (id==null){
            throw new CustomerException(MessageCode.ID_EMPTY);
        }
        PolicyListVo policyById = policyService.findPolicyById(id);
        return ResultUtils.buildSuccess(policyById);
    }


}
